﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum TargetofUpgrade
{
    LevelMaster,
    EntityStatManager,
    EndLevelStatManager
}

public class UpgradeManager : MonoBehaviour {

    public GameObject levelManager;
    public GameObject statManager;
    public GameObject endLevelStat;

    //GUI variables
    private int width = 50;
    private int height = 50;
    private int spaceBetweenColumn = 20;
    private int spaceBetweenLine = 60;

    private bool showUpgrade;
    public bool ShowUpgrade
    {
        get { return showUpgrade; }
        set { showUpgrade = value; }
    }


    //Upgrade screen variables
    private Rect upgradeBackGroundRect;
    private Rect descriptionRect;
    private int upgradeWidthOffset;
    private int upgradeHeightOffset;
    private int upgradeDescriptionOffset = 50;
    private Rect backToMainMenuRect;
    private Rect resumeButtonRect;

    private int upgradePoints;
    public int UpgradePoints
    {
        get { return upgradePoints; }
        set { upgradePoints = value; }
    }

	// Use this for initialization
	void Start () {
        ShowUpgrade = false;

        UpgradePoints = 0;
        upgradeWidthOffset = 25;
        upgradeHeightOffset = 25;
        upgradeBackGroundRect = new Rect(upgradeWidthOffset, upgradeHeightOffset, Screen.width - 2 * upgradeWidthOffset, Screen.height - 2 * upgradeHeightOffset);
        descriptionRect = new Rect(Screen.width / 2 - (Screen.width / 4) , Screen.height / 8, Screen.width / 2, Screen.height /8);

        resumeButtonRect = new Rect(Screen.width / 2 - 30, Screen.height - upgradeHeightOffset - 50, 75, 40);
        backToMainMenuRect = new Rect(Screen.width - upgradeWidthOffset - 25, upgradeHeightOffset, 25, 25);

        //Create the rect for the GUI for each upgrade in the List
        int heightIndex = 0;
        foreach(Transform child in transform)
        {
            UpgradeTree tree = child.GetComponent<UpgradeTree>();
            foreach (Upgrade upgrade in tree.AvailableUpgrades)
            {
                upgrade.GUIRect = new Rect(Screen.width / 4 + upgrade.TreeLevel * (width + spaceBetweenColumn), heightIndex * spaceBetweenLine + Screen.height / 3, width, height);
            }
            heightIndex++;
        }
        EventManager<int>.AddListener("level up", onLevelUp);
        EventManager<Upgrade>.AddListener("buy upgrade", onUpgradeBuy);
	}
	
	// Update is called once per frame
	void Update () {
	}

    void OnGUI()
    {
        
    }

    public bool printUpgradeGUI()
    {
        GUI.Box(upgradeBackGroundRect, new GUIContent("Remaining points : " + UpgradePoints, "Box hover"));
        //Print the upgrades' icones depending on weither they are upgradable or not + check for hover
        int Treeindex = 0; //Used to keep track on the tree from wich the description has to be printed
        foreach (Transform child in transform)
        {
            UpgradeTree tree = child.GetComponent<UpgradeTree>();
            for (int index = 0; index < tree.AvailableUpgrades.Count; ++index)
            {
                if (tree.AvailableUpgrades[index].isLevelMax())
                {
                    GUI.Label(tree.AvailableUpgrades[index].GUIRect, new GUIContent("IMG", "upgrade description " + index + Treeindex));
                }
                else if (!tree.AvailableUpgrades[index].canBuy(tree.InvestedPoints))
                {
                    GUI.Box(tree.AvailableUpgrades[index].GUIRect, new GUIContent("IMG", ""));
                }
                else
                {
                    if (GUI.Button(tree.AvailableUpgrades[index].GUIRect, new GUIContent("IMG", "upgrade description " + index + Treeindex)))
                    {
                        useUpgrade(tree.AvailableUpgrades[index]);
                        tree.InvestedPoints++;
                    }
                }
            }

            //Print description on hover
            for (int index = 0; index < tree.AvailableUpgrades.Count; ++index)
            {
                if (GUI.tooltip == "upgrade description " + index + Treeindex)
                {
                    //Create the description label and place it at the right position
                    GUI.Label(descriptionRect, tree.AvailableUpgrades[index].Description);
                }
            }
            if (GUI.Button(backToMainMenuRect, "X") || GUI.Button(resumeButtonRect, "Resume"))
            {
                ShowUpgrade = false;
                return false;
            }
            Treeindex++;
        }
        return true;
    }

    public void useUpgrade(Upgrade upgrade)
    {
        if(UpgradePoints > 0)
        {
            EventManager<Upgrade>.Raise("buy upgrade", upgrade);
            switch (upgrade.UpgradeType)
            {
                case TargetofUpgrade.LevelMaster:
                    upgrade.buy(levelManager);
                    break;
                case TargetofUpgrade.EntityStatManager:
                    upgrade.buy(statManager);
                    break;
                case TargetofUpgrade.EndLevelStatManager:
                    upgrade.buy(endLevelStat);
                    break;
            }
        }
    }

    private void onLevelUp(int level)
    {
        UpgradePoints += 3;
    }

    private void onUpgradeBuy(Upgrade upgrade)
    {
        upgradePoints--;
    }
}
